import org.jgrapht.GraphPath;
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleWeightedGraph;

/**
 * @author 20586
 * @ClassName Test021
 * @Date 2023/10/12 15:48
 * @Version 1.0
 */
public class Test21 {
    public static void main(String[] args) {
        SimpleWeightedGraph<String, DefaultWeightedEdge> graph = new SimpleWeightedGraph<>(DefaultWeightedEdge.class);
        String[] vertices = new String[]{"A", "B", "C", "D", "E", "F","G"};
        for (String vertex : vertices) {
            graph.addVertex(vertex);
        }
        graph.setEdgeWeight(graph.addEdge("A", "B"), 12);
        graph.setEdgeWeight(graph.addEdge("A", "D"), 6);
        graph.setEdgeWeight(graph.addEdge("A", "E"), 15);
        graph.setEdgeWeight(graph.addEdge("B", "E"), 5);
        graph.setEdgeWeight(graph.addEdge("E", "F"), 7);
        graph.setEdgeWeight(graph.addEdge("D", "C"), 11);
        graph.setEdgeWeight(graph.addEdge("C", "F"), 7);
        graph.setEdgeWeight(graph.addEdge("C", "G"), 3);
        DijkstraShortestPath<String, DefaultWeightedEdge> dijkstra = new DijkstraShortestPath<>(graph);
        System.out.println(dijkstra.getPath("A", "F"));
        GraphPath<String, DefaultWeightedEdge> path = dijkstra.getPath("A", "F");
        System.out.println("最短路径的值:" + path.getWeight());
    }
}
